<?php

class models_ProjectMapper extends models_MapperBase
{
    public static $_dbTable = 'models_DbTable_Project';

//----------------------------------------------------------------------------------------------------

    protected static function _initItem($oRow)
    {
        if (null === $oRow)
        {
            return null;
        }

        $oItem = new models_Project();


        if (isset($oRow->id))
            $oItem->id          = $oRow->id;
        if (isset($oRow->name))
            $oItem->name        = $oRow->name;
        if (isset($oRow->base))
            $oItem->base        = $oRow->base;   
        if (isset($oRow->start_date))
            $oItem->start_date      = $oRow->start_date;
        if (isset($oRow->end_date))
            $oItem->end_date        = $oRow->end_date;               
        if (isset($oRow->project_num))
            $oItem->project_num     = $oRow->project_num; 
        if (isset($oRow->time_off))
            $oItem->time_off        = $oRow->time_off;   
        if (isset($oRow->billable))
            $oItem->billable        = $oRow->billable;               
            
        return $oItem;
    }

//----------------------------------------------------------------------------------------------------

    public static function getAll($page = 1, $countItemsOnPage = 10, $base = false)
    {
        $db = self::_getDbTable(self::$_dbTable);
        $select  = $db->select();

        $select->from($db, array('*'));
        $select->setIntegrityCheck(false);
        if (! $base)
        {
        	$select->where('base = ?', 0);
        }
        
        $select->where('in_list = ?', 1);
        $select->order('id DESC');
//      echo $select; die;

        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setItemCountPerPage($countItemsOnPage)
                  ->setCurrentPageNumber($page);

        $result = array();
        foreach ($paginator as $page)
        {
            $result[] = (object)$page;
        }

        $ret['array']     = $result;
        $ret['paginator'] = $paginator;

        return $ret;
    }

//----------------------------------------------------------------------------------------------------

    public static function findByIdUserOLD($idUser, $startDate, $endDate)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        
        $select->joinRight('user_to_project', 'project.id = user_to_project.id_project ', array('*'));

        $select->where('user_to_project.id_user = ?', $idUser);
        $select->orWhere('user_to_project.endless = ?', 1);
        $select->order(array('project.project_num', 'id'));
      
//        echo $select ;die;
      
        $result = $DbTable->fetchAll($select);
        $res    = $result->toArray();
        
        $select = $DbTable->select();
        $select->from($DbTable, array('*'));
        $select->where('project.base = ?', 1);
        $select->order(array('project.project_num', 'id'));
        
        $result = $DbTable->fetchAll($select);
        $res2   = $result->toArray();
          
        $r_res  = array_merge($res, $res2);
        
        return $r_res;
    }
    
//----------------------------------------------------------------------------------------------------

    public static function findByIdUser($idUser, $startDate, $endDate)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
/*
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        
        $select->joinRight('user_to_project', 'project.id = user_to_project.id_project ', array('*'));

        $select->where('user_to_project.id_user = ?', $idUser);
        $select->orWhere('user_to_project.endless = ?', 1);
        $select->order(array('project.project_num', 'id'));
      
//        echo $select ;die;
      
        $result = $DbTable->fetchAll($select);
        $res    = $result->toArray();
*/      
        $sql = "
        SELECT `project`.*, `user_to_project`.* 
            FROM `project` RIGHT JOIN `user_to_project` ON project.id = user_to_project.id_project 
            WHERE (user_to_project.id_user = $idUser) 
             AND 
                (
                    (`user_to_project`.start_date <='" . $startDate . "' AND `user_to_project`.end_date >= '" . $endDate . "')
                        OR  `user_to_project`.endless = 1
                )
            ORDER BY `project`.`project_num` ASC, `id` ASC";  
        $res = $DbTable->getAdapter()->fetchAll($sql, array());
        
        $select = $DbTable->select();
        $select->from($DbTable, array('*'));
        $select->where('project.base = ?', 1);
        $select->order(array('project.project_num', 'id'));
        
        $result = $DbTable->fetchAll($select);
        $res2   = $result->toArray();
          
        $r_res  = array_merge($res, $res2);
        
        return $r_res;
    }
    
//----------------------------------------------------------------------------------------------------

    public static function findById($id)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));

        $select->where('id = ?', $id);

//      echo $select;die;
        $result = $DbTable->fetchRow($select);

        return self::_initItem($result);
    }

//----------------------------------------------------------------------------------------------------
    
    public static function findByName($name)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));

        $select->where('name = ?', $name);

//      echo $select;die;
        $result = $DbTable->fetchRow($select);

        return self::_initItem($result);
    }
    
//----------------------------------------------------------------------------------------------------

    public static function save($data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $result = $DbTable->insert($data);
        return $result;
    }

//----------------------------------------------------------------------------------------------------

    public static function edit($id, $data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $Update = $DbTable->update($data,  'id = ' . $id);

        return $Update;
    }

//----------------------------------------------------------------------------------------------------

    /**
     * Deletes project from table
     *
     * @param $iId
     */
    public static function delete($iId)
    {
        $oDbTable = self::_getDbTable(self::$_dbTable);
        $where = $oDbTable->getAdapter()->quoteInto('id = ?', $iId, Zend_Db::INT_TYPE);
        $oDbTable->delete($where);
    }

//
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------

}
